package com.tianxin.workbao.workBao.adapter;

import android.content.Context;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.tianxin.workbao.workBao.Activity.BaseActivity;
import com.tianxin.workbao.workBao.R;
import com.tianxin.workbao.workBao.bean.Transfermoney;
import com.tianxin.workbao.workBao.dataProvide.TransferMoneyProvide;
import com.tianxin.workbao.workBao.holder.EmptyHolder;
import com.tianxin.workbao.workBao.listener.UpdateListViewListener;
import com.tianxin.workbao.workBao.utils.common.TimeUtils;
import com.tianxin.workbao.workBao.utils.common.ToastUtils;
import com.tianxin.workbao.workBao.view.ExpandView;

import java.util.List;

/**
 * Created by hacker on 2017/7/26.
 */

public class TransferMoneyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int TYPE_EMPTY = 0;
    private static final int TYPE_DEFAULT = 1;
    private List<Transfermoney> transfermoneyList;
    private Context mContext;
    private int mPosition;


    public TransferMoneyAdapter(List<Transfermoney> list, Context context) {
        this.transfermoneyList = list;
        this.mContext = context;
    }

    @Override
    public int getItemViewType(int position) {
        if (transfermoneyList.size() > 0) {
            return TYPE_DEFAULT;
        } else {
            return TYPE_EMPTY;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        if (viewType == TYPE_DEFAULT) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.transfer_money_item, parent, false);
            return new ViewHolder(view);
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty, parent, false);
            return new EmptyHolder(view);
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            mPosition = position;
            ViewHolder viewHolder = (ViewHolder) holder;
            viewHolder.BindViewData(transfermoneyList.get(mPosition));
            viewHolder.setViewListener(transfermoneyList.get(mPosition));
        }
    }

    @Override
    public int getItemCount() {
        if (transfermoneyList.size() > 0) {
            return transfermoneyList.size();
        } else {
            return 1;
        }
    }

    public int getItemColumnSpan(int position) {
        switch (getItemViewType(position)) {
            case TYPE_DEFAULT:
                return 1;
            default:
                return 1;
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        ImageView arrowIV;
        ExpandView expandView;
        TextView numberTV;
        TextView statusLabelTV;
        TextView moneyTV;
        TextView timeTV;
        TextView remarkTV;
        TextView addUserTV;
        TextView oauthUserTV;
        TextView bankNameTV;
        TextView bankNumberTV;
        TextView carNumberTV;
        Button checkBtn;


        public ViewHolder(View itemView) {
            super(itemView);
            numberTV = (TextView) itemView.findViewById(R.id.number);
            arrowIV = (ImageView) itemView.findViewById(R.id.arrow);
            expandView = (ExpandView) itemView.findViewById(R.id.expandView);
            statusLabelTV = (TextView) itemView.findViewById(R.id.status_label);
            timeTV = (TextView) itemView.findViewById(R.id.time);
            moneyTV = (TextView) itemView.findViewById(R.id.money);
            remarkTV = (TextView) itemView.findViewById(R.id.remark);
            bankNameTV = (TextView) itemView.findViewById(R.id.bank_name);
            bankNumberTV = (TextView) itemView.findViewById(R.id.bank_number);
            addUserTV = (TextView) itemView.findViewById(R.id.add_user);
            oauthUserTV = (TextView) itemView.findViewById(R.id.oauth_user);
            carNumberTV = (TextView) itemView.findViewById(R.id.car_number);
            checkBtn = (Button) itemView.findViewById(R.id.check_btn);
        }

        public void BindViewData(Transfermoney transfermoney) {
            numberTV.setText(transfermoney.getNumber());
            carNumberTV.setText(transfermoney.getPlatenumbers());
            if (transfermoney.getStatus() == 1) {
                checkBtn.setVisibility(View.GONE);
                statusLabelTV.setText("到账");
                timeTV.setText(TimeUtils.parseDate(transfermoney.getTrueaddtime()));
                oauthUserTV.setText("转账：" + transfermoney.getOptionusername() + "\n"
                        + TimeUtils.parseDateTime(transfermoney.getTrueaddtime()));
            } else {
                checkBtn.setVisibility(View.VISIBLE);
                statusLabelTV.setText("未到");
                timeTV.setText(TimeUtils.parseDate(transfermoney.getAddtime()));
                oauthUserTV.setText("未转账");
                oauthUserTV.setTextColor(mContext.getResources().getColor(R.color.red_btn_bg_color));
            }
            addUserTV.setText("添加：" + transfermoney.getAddusername() + "\n"
                    + TimeUtils.parseDateTime(transfermoney.getAddtime()));
            bankNameTV.setText(transfermoney.getBankname());
            bankNumberTV.setText(transfermoney.getCardHolder() + "  " + transfermoney.getBankcardnumber());
            moneyTV.setText("金额" + transfermoney.getMoney());
            int length = transfermoney.getRemark().length();
            if (length > 1 && transfermoney.getRemark() != "0") {
                remarkTV.setText("备注：" + transfermoney.getRemark());
                remarkTV.setVisibility(View.VISIBLE);
            } else {
                remarkTV.setVisibility(View.GONE);
            }
        }

        public void setViewListener(final Transfermoney transfermoney) {
            arrowIV.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    switch (view.getId()) {
                        case R.id.arrow:
                            if (expandView.isExpand()) {
                                expandView.collapse();
                                arrowIV.setImageResource(R.drawable.ic_arrow_drop_down_black_24dp);
                            } else {
                                expandView.expand();
                                arrowIV.setImageResource(R.drawable.ic_arrow_drop_up_black_24dp);
                            }
                            break;
                    }
                }
            });
            checkBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (view.getId() == R.id.check_btn) {
                        getTransferMoneyProvide().check(transfermoney);
                    }
                }
            });
        }


        public TransferMoneyProvide getTransferMoneyProvide() {
            TransferMoneyProvide provide = new TransferMoneyProvide(mContext, new UpdateListViewListener() {
                @Override
                public void onRefreshData(Object result) {

                }

                @Override
                public void onLoadMoreData(Object result) {

                }

                @Override
                public void showMessage(String msg) {
                    if (msg == "ok") {
                        expandView.collapse();
                        transfermoneyList.remove(mPosition);
                        notifyDataSetChanged();
                        ToastUtils.showLong("操作成功");
                    } else if (msg.contains("failure")) {
                        ToastUtils.showLong("操作失败！\n " + msg.substring(msg.indexOf("failure") + 1, msg.length()));
                    } else {
                        ToastUtils.showLong(msg);
                    }
                }

                @Override
                public void showProgress() {
                    BaseActivity activity = (BaseActivity) TransferMoneyAdapter.this.mContext;
                    activity.getSweetAlertDialog(activity).showDialog("", 1200);
                }

                @Override
                public void hideProgress() {
                    BaseActivity activity = (BaseActivity) TransferMoneyAdapter.this.mContext;
                    activity.getSweetAlertDialog(activity).dismiss();
                }
            }, null);
            return provide;
        }
    }
}
